var mailUtil = require('../utility/mailUtil')
var User = require('../handlers/userHandler.js')
var ApplyManage = require('../handlers/applyManageHandler')
var Suggest = require('../handlers/suggestHandler')

var config = require('../config/config')

module.exports = {

    register: function(req, res) {
        var body = req.body

        User.findByName(body.name, function(err, user) {
            if (user) {
                return res.json({
                    code: 122,
                    msg: '用户名已存在'
                })
            }
            User.insert(body, function(err, user) {
                if (err) {
                    console.log('userController register fail. err = ' + err)
                    return res.json({
                        code: 177,
                        msg: '注册失败'
                    })
                } else {
                    return res.json({
                        code: 100,
                        data: {
                            user: user
                        }
                    })
                }
            })
        })

    },
    login: function(req, res) {
        var query = req.query
        User.findByName(query.name, function(err, user) {
            if (err) {
                return res.json({
                    code: 177,
                    msg: err
                })
            } else if (user) {

                if (user.password == query.password) {
                    return res.json({
                        code: 100,
                        data: {
                            user: user
                        }
                    })
                } else {
                    return res.json({
                        code: 122,
                        msg: '密码错误'
                    })
                }
            } else {
                return res.json({
                    code: 112,
                    msg: '该用户未注册'
                })
            }
        })

    },
    getAll: function(req, res) {
        if (!(req.session.adminId && req.session.adminId == config.admin.adminId)) {
            return res.json({
                code: 122,
                msg: '无权进行此操作'
            })
        }
        User.find(req.query, function(err, users) {
            if (err) {
                console.log('userController getAll fail. err = ' + err)
                return res.json({
                    code: 177,
                    msg: '获取用户列表出错'
                })
            }
            if (users.length == 0) {
                return res.json({
                    code: 112,
                    msg: '无数据'
                })
            }
            return res.json({
                code: 100,
                data: {
                    list: users
                }
            })
        })

    },
    getUserInfo: function(req, res) {
        var id = req.params.id

        if (!id || id.length != 16) {
            return res.json({
                code: 122,
                msg: '用户id错误'
            })
        }

        User.findById(id, function(err, user) {
            if (err) {
                console.log('userController getUserInfo fail. err = ' + err)
                return res.json({
                    code: 177,
                    msg: err
                })
            }
            if (!user) {
                return res.json({
                    code: 112,
                    msg: '没有找到该用户'
                })
            }
            return res.json({
                code: 100,
                msg: '查找成功',
                data: {
                    user: user
                }
            })
        })

    },
    updateUserInfo: function(req, res) {
        var id = req.params.id

        if (!id || id.length != 16) {
            return res.json({
                code: 122,
                msg: '用户id错误'
            })
        }

        User.findByIdAndUpdate(id, {
            handlerType: 'update',
            content: req.body.user
        }, function(err, user) {
            if (err) {
                console.log('userController updateUserInfo fail. err = ' + err)
                return res.json({
                    code: 177,
                    msg: err
                })
            }
            return res.json({
                code: 100,
                msg: '更新成功',
                data: {
                    user: user
                }
            })
        })

    },
    //  获取验证码
    getVerifyCode: function(req, res) {
        var email = req.query.email
        var code = Math.random().toString().slice(-4)
        req.session.verifyCode = code
        req.session.bindEmail = email
        mailUtil.sendEmail(email, '验证码： ' + code, '<h1>尊敬的用户</h1><p>您的验证码是：' + code + '，请妥善保管。</p>')
        return res.json({
            code: 100,
            msg: '验证码已成功'
        })
    },
    changePassword: function(req, res) {
        var id = req.params.id
        var body = req.body
        var user = body.user
        var newPassword = body.newPassword

        if (!id || id.length != 16) {
            return res.json({
                code: 122,
                msg: '用户id错误'
            })
        }

        user.password = newPassword

        User.findByIdAndUpdate(id, {
            handlerType: 'update',
            content: user
        }, function(err, newUser) {
            if (err) {
                console.log('userController changePassword fail. err = ' + err)
                return res.json({
                    code: 177,
                    msg: err
                })
            }
            return res.json({
                code: 100,
                msg: '更新成功'
            })
        })


    },
    applyManage: function(req, res) {
        var body = req.body

        User.findById(body.handler.id, function(err, user) {
            if (err) {
                console.log('userController applyManage fail. err = ' + err)
                return res.json({
                    code: 177,
                    msg: err
                })
            }
            if (!user) {
                return res.json({
                    code: 122,
                    msg: '未找到用户信息'
                })
            }
            if (+user.identity > 0) {
                return res.json({
                    code: 112,
                    msg: '已经是管理了，无需申请'
                })
            }
            ApplyManage.insert(body, function(err, apply) {
                if (err) {
                    console.log('userController applyManage fail. err = ' + err)
                    return res.json({
                        code: 177,
                        msg: err
                    })
                }
                return res.json({
                    code: 100,
                    msg: '申请成功'
                })
            })
        })

    },
    suggest: function(req, res) {
        var body = req.body

        Suggest.insert(body, function(err, apply) {
            if (err) {
                console.log('userController suggest fail. err = ' + err)
                return res.json({
                    code: 177,
                    msg: err
                })
            }
            return res.json({
                code: 100,
                msg: '提交成功'
            })
        })
    }
}